
package com.choosefine.it.worker.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.common.def.CommonConstant;
import com.choosefine.it.common.def.ResultCode;
import com.choosefine.it.common.exception.BusinessException;
import com.choosefine.it.common.model.CustomRestTemplate;
import com.choosefine.it.common.utils.URLHandlerUtils;
import com.choosefine.it.worker.model.WorkDetailInfo;
import com.choosefine.it.worker.model.WorkerHistoryInfo;
import com.choosefine.it.worker.model.WorkerItemsRelation;
import com.choosefine.it.worker.model.vo.WorkMyJobVO;
import com.choosefine.it.worker.model.vo.WorkerHistoryVO;
import com.choosefine.it.worker.service.MyWorksManagerService;
import com.github.pagehelper.Page;
import com.google.gson.Gson;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 工作空间管理  如：我的工作管理相关功能 及 考勤 控制层入口
 * @auth liaozhanggen 2016-11-21 下午7:48:31
 *
 */
@Api(value="工人-工作管理",description="廖仉根",protocols="http",consumes=MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/myspace")
public class WorkSpaceMgtController extends BaseController {

	@Autowired
	MyWorksManagerService myWorksManagerService;
	
	@Value("#{yaml.get('basedata.userinfo.findUsersByRealName')}")
	final String projectURL = null;

	@Autowired
	CustomRestTemplate restTemplate;

	private Gson gson = new Gson();

	/**
	 * 工人角色 - 工作管理 - 我的工作 - 工作详情
	 * @throws BusinessException 
	 * @auth liaozhanggen 2016-12-1 下午4:27:46
	 */
	@ResponseBody
	@ApiOperation("工人角色 - 工作管理 - 我的工作 - 工作详情")
	@RequestMapping(method=RequestMethod.GET,value="/work/detail/{userCode}/{relationId}",produces=MediaType.APPLICATION_JSON_VALUE)
	public String queryWorkManagementWorkDetialByUCode(	@PathVariable(value="userCode",required = true) String userCode,
														@PathVariable(value="relationId",required = true) Integer relationId) throws BusinessException{
		String result = null;
		WorkDetailInfo wdi=myWorksManagerService.queryWorkerWorkDetialInfo(userCode,relationId);
		result = responseSuccess(wdi);
		return result; 
	}
	/**
	 * 从控制台-入口 添加维护 工人 和 班组、项目 、工种的关系 支持批量操作
	 * @throws BusinessException 
	 * @auth liaozhanggen 2016-11-21 下午7:53:20
	 */
	@ResponseBody
	@ApiOperation("控制台-添加维护 工人 和 班组、项目 、工种的关系 支持批量操作(我的工作)")
	@RequestMapping(method=RequestMethod.POST,value="/add/worker_items",consumes=MediaType.APPLICATION_JSON_VALUE)
	public String addWorkManagementList(@RequestBody List<WorkerItemsRelation> wItemsRelations) throws BusinessException{
		String result = null;
		if (null == wItemsRelations || wItemsRelations.size() == 0) {
			throw new BusinessException(ResultCode.FAILED,CommonConstant.TEAM_RECRUITMENT_ID_IS_NULL);
		}
		int num = myWorksManagerService.addBatchWorkerItemsRelation(wItemsRelations);
		result = num > 0 ? responseSuccess() : responseFail();
		return result; 
	}
	
	/**
	 * app 工人角色 - 我的工作管理 列表
	 * @throws BusinessException 
	 * 
	 * @auth liaozhanggen 2016-11-22 下午1:24:14
	 */
	@ResponseBody
	@ApiOperation(value = "工作管理-我的工作列表",response = WorkMyJobVO.class)
	@RequestMapping(method=RequestMethod.POST,value="/query/worker_items/{userCode}",consumes=MediaType.APPLICATION_JSON_VALUE)
	public String queryWorkManagementPageByUCode(@RequestBody PageDTO page,@PathVariable(value="userCode",required = true) String userCode) throws BusinessException{
			
		//调用我参与的工作 列表接口
		Page<WorkerItemsRelation> wirs = myWorksManagerService.queryWorkerItemsRelationPageByUCode(userCode,page);
		
		if(wirs==null) return responseFail(ResultCode.MSG_DATA_ISEMPTY);
		Page<WorkMyJobVO> lwvo = workTransferVO(wirs);	//返回工作列表结果处理
		lwvo.setPages(wirs.getPages());
		
		return responseSuccess(lwvo);
	}
	
	/**
	 * //返回工作列表结果处理
	 * 
	 * @author liaozhanggen 2017年1月18日
	 */
	private Page<WorkMyJobVO> workTransferVO(Page<WorkerItemsRelation> wirs) {
		Page<WorkMyJobVO> lwvo=new Page<>();
		WorkMyJobVO wvo= null;
		for (WorkerItemsRelation temp : wirs) {
			String projectUserCode=null;
			if(temp.getProjectInfo().getContractorName()!=null){
				StringBuffer url=URLHandlerUtils.getUrlBuffer(shixiaobaoURL, projectURL).append(temp.getProjectInfo().getContractorName());
				Map maps= restTemplate.getForObject(url.toString(), Map.class);
				Map array= (Map) maps.get("data");
				projectUserCode = (String) array.get("userCode");
			}
			wvo= gson.fromJson(gson.toJson(temp),WorkMyJobVO.class);
			wvo.setOpenDate(temp.getCtime());
			wvo.setProjectAddress(temp.getProjectInfo().getProjectAddress());
			wvo.setProjectName(temp.getProjectInfo().getProjectName());
			wvo.setRelationId(temp.getId());
			wvo.setConstruction_unit(temp.getProjectInfo().getConstructionUnit());
			if(temp.getItemInfo()!=null){
			wvo.setTeamContactor(temp.getItemInfo().getUserName());
//			wvo.setTeamTypeName(temp.getItemInfo().getTeamTypeName());
			wvo.setTeamName(temp.getTeamName());
			}
			wvo.setTeamUserCode(temp.getUserCode());
			if(temp.getWorkType()!=null){
				wvo.setWorkTypeName(temp.getWorkType().getName());
			}
			lwvo.add(wvo);
			wvo = null;
		}
		
		return lwvo;
	}
	
	/**
	 * 工人角色 - 我的 - 做过的历史工作
	 * @throws BusinessException 
	 * @auth liaozhanggen 2016-12-1 下午4:27:46
	 */
	@ResponseBody
	@ApiOperation("工人角色 - 我的 - 做过的历史工作")
	@RequestMapping(method=RequestMethod.POST,value="/history/work/{userCode}",consumes=MediaType.APPLICATION_JSON_VALUE)
	public String queryWorkManagementHistoryPageByUCode(@RequestBody PageDTO page,
													 	@PathVariable(value="userCode",required = true) String userCode) throws BusinessException{
		String result = null;
		//调用我参与的工作 列表接口
		Page<WorkerHistoryInfo> historyInfos = myWorksManagerService.queryWorkerHistoryWorkInfo(userCode,page);
		WorkerHistoryVO whvo=new WorkerHistoryVO();
		whvo.setWorkerHistoryInfo(historyInfos.toPageInfo().getList());
		whvo.setSum(historyInfos.toPageInfo().getTotal());
		whvo.setPageCount(historyInfos.getPages());
		result = responseSuccess(whvo);
		return result;
	}
}
